//
//  WLoginController.m
//  WeChat
//
//  Created by Yanting on 16/7/13.
//  Copyright © 2016年 Yanting. All rights reserved.
//

#import "WLoginController.h"
#import "AppDelegate.h"

@interface WLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *textFieldUser;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPassword;

- (IBAction)LoginBtnClick:(id)sender;
@end

@implementation WLoginController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)LoginBtnClick:(id)sender {
    if (self.textFieldUser.text.length == 0 || self.textFieldPassword.text.length == 0) {
        return;
    }
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setObject:self.textFieldUser.text forKey:@"user"];
//    [defaults setObject:self.textFieldPassword.text forKey:@"password"];
//    [defaults synchronize];
    [WAccount shareAccount].user = self.textFieldUser.text;
    [WAccount shareAccount].pwd = self.textFieldPassword.text;
//    __weak typeof (self) selfVC = self;
//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [[WXMPPTool sharedWXMPPTool] xmppLogin:^(XMPPResultType resultType){
        dispatch_async(dispatch_get_main_queue(), ^{

            if (resultType == XMPPResultTypeLoginSuccess) {
                NSLog(@"%s login success",__func__);
                [WAccount shareAccount].login = YES;
                [self changeToMain];
                
                [[WAccount shareAccount] saveToSandBox];
            }else{
                NSLog(@"%s login failure",__func__);
            
            }
        });
    }];
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}

-(void)changeToMain{
    dispatch_async(dispatch_get_main_queue(), ^{
        id vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateInitialViewController];
        [UIApplication sharedApplication].keyWindow.rootViewController = vc;
    });
    
}
@end
